<?php

namespace app\admin\controller;
use org\Tree;
class AuthRule extends base
{
    //前置方法
    protected $beforeActionList = [
        'map'  =>  ['only'=>'index'],
        'auth_rule' => ['only' => 'add,edit'],

    ];

    /**
     * 搜索
     */
    protected function map(){
        if ($this->request->isGet()) {
            $data = $this->request->param();
            $this->datas = array();
            if (!empty($data['start_time']) && !empty($data['end_time']) && (strtotime($data['start_time']) < strtotime($data['end_time']))) {
                $this->datas ['create_time'] = [
                    ['>', strtotime($data['start_time'])],
                    ['<', strtotime($data['end_time'])]
                ];
            }
            if (!empty($data['cid'])) {
                $this->datas ['cid'] = $data['cid'];
            }
            if (!empty($data['title'])) {
                $this->datas ['title'] = ['like', '%' . $data['title'] . '%'];
            }
        }
    }

    public function auth_rule(){
        $rule = model('auth_rule')->select();
		$rule = Tree::Forlevel($rule);
        $this->assign('auth_rule', $rule);

    }
	
	    //列表
    public function index()
    {
		
        $rule = model('auth_rule')->select();
        $view = Tree::Forlevel($rule);
		$this->assign('list', $view);
		$this->assign('addcid','');
		$this->assign('num',model('auth_rule')->count());
        return $this->fetch();
    }



    /**
     * 保存
     */
    public function save()
    {
        if (!$this->request->isPost()) {
            return ajax_info('请求失败');
        }
        $data = $this->request->post();
//        $result = $this->validate($data, 'AuthGroup');
//        if (true !== $result) {
//            return ajax_info($result);
//        }
        if(!empty($data['id'])){
            return $this->update($data);
        }

        $res = $this->model->allowField(true)->save($data);
        if ($res) {
            return ajax_info('保存成功','y');
        } else {
            return ajax_info('添加失败');
        }
    }
}
